package top.zhangjianyong.blog.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import top.zhangjianyong.blog.enums.RespCode;
import top.zhangjianyong.blog.entity.dto.FileDTO;
import top.zhangjianyong.blog.entity.dto.WeiboDTO;
import top.zhangjianyong.blog.entity.po.Weibo;
import top.zhangjianyong.blog.mapper.WeiboMapper;
import top.zhangjianyong.blog.params.weibo.*;
import top.zhangjianyong.blog.response.ApiResponse;
import top.zhangjianyong.blog.response.PageDTO;
import top.zhangjianyong.blog.service.WeiboFileService;
import top.zhangjianyong.blog.service.WeiboService;
import top.zhangjianyong.blog.utils.EntityUtil;

@Slf4j
@Service
public class WeiboServiceImpl extends ServiceImpl<WeiboMapper, Weibo> implements WeiboService {

  private final WeiboFileService weiboFileService;

  public WeiboServiceImpl(@Lazy WeiboFileService weiboFileService) {
    this.weiboFileService = weiboFileService;
  }

  @Override
  public ApiResponse page(WeiboPageParam param) {
    Page<Weibo> page =
        lambdaQuery()
            .like(StrUtil.isNotBlank(param.keyword()), Weibo::getContent, param.keyword())
            .eq(null != param.status(), Weibo::getStatus, param.status())
            .eq(Weibo::getIsDel, 0)
            .orderByDesc(Weibo::getCreateTime)
            .page(new Page<>(param.pageNum(), param.pageSize()));

    List<WeiboDTO> records =
        page.getRecords().stream()
            .map(
                weibo -> {
                  WeiboDTO weiboDTO = new WeiboDTO();
                  BeanUtils.copyProperties(weibo, weiboDTO);
                  return weiboDTO;
                })
            .toList();

    for (WeiboDTO weiboDTO : records) {
      List<FileDTO> fileDTOS = weiboFileService.listFileByWeiboId(weiboDTO.getId());
      weiboDTO.setFiles(fileDTOS);
    }

    return ApiResponse.success(PageDTO.getPage(page, records));
  }

  @Override
  public ApiResponse saveOrUpdate(WeiboSaveOrUpdateParam param) {
    Weibo weibo = new Weibo();
    BeanUtils.copyProperties(param, weibo);
    weibo.setStatus(0);
    if (!saveOrUpdate(weibo)) {
      return ApiResponse.fail(RespCode.WEIBO_SAVE_OR_UPDATE_ERROR);
    }

    weiboFileService.updateFiles(weibo.getId(), param.files());

    return ApiResponse.success(weibo);
  }

  @Override
  public ApiResponse push(WeiboPushParam param) {
    return lambdaUpdate()
            .eq(Weibo::getId, param.id())
            .set(Weibo::getStatus, param.status())
            .update()
        ? ApiResponse.success()
        : ApiResponse.fail(RespCode.WEIBO_PUSH_ERROR);
  }

  @Override
  public ApiResponse remove(WeiboRemoveParam param) {
    return lambdaUpdate().eq(Weibo::getId, param.id()).set(Weibo::getIsDel, 1).update()
        ? ApiResponse.success()
        : ApiResponse.fail(RespCode.WEIBO_REMOVE_ERROR);
  }

  @Override
  public ApiResponse portalPage(WeiboPageParam param) {
    Page<Weibo> page =
        lambdaQuery()
            .eq(Weibo::getStatus, 1)
            .eq(Weibo::getTopFlag, 0)
            .orderByDesc(Weibo::getCreateTime)
            .page(new Page<>(param.pageNum(), param.pageSize()));
    PageDTO<WeiboDTO> p = PageDTO.getPage(page, WeiboDTO.class);
    for (WeiboDTO weiboDTO : p.getRecords()) {
      List<FileDTO> fileDTOS = weiboFileService.listFileByWeiboId(weiboDTO.getId());
      weiboDTO.setFiles(fileDTOS);
    }
    return ApiResponse.success(p);
  }

  @Override
  public ApiResponse top(WeiboTopParam param) {
    return lambdaUpdate()
            .eq(Weibo::getId, param.id())
            .set(Weibo::getTopFlag, param.topFlag())
            .update()
        ? ApiResponse.success()
        : ApiResponse.fail(RespCode.WEIBO_TOP_ERROR);
  }

  @Override
  public ApiResponse topList() {
    List<Weibo> list =
        lambdaQuery()
            .eq(Weibo::getStatus, 1)
            .eq(Weibo::getTopFlag, 1)
            .orderByDesc(Weibo::getCreateTime)
            .list();
    List<WeiboDTO> weiboDTOS = EntityUtil.entity2DTO(list, WeiboDTO.class);
    for (WeiboDTO weiboDTO : weiboDTOS) {
      List<FileDTO> fileDTOS = weiboFileService.listFileByWeiboId(weiboDTO.getId());
      weiboDTO.setFiles(fileDTOS);
    }
    return ApiResponse.success(weiboDTOS);
  }
}
